#region Using References

using System;
using System.Linq;
using Beaker.OpenCube.CubeManager.Model.Framework;
using Beaker.OpenCube.CubeManager.Model.Interfaces;

#endregion

namespace Beaker.OpenCube.CubeManager.Model.Repositories
{
	internal static class RepositoryFactory
	{
		public static IRepository<TEntity> Create<TEntity>( CubeManagerUnitOfWork unitOfWork )
			where TEntity : Entity
		{
			Type repositoryType =
				(
					from type in typeof( ServerRepository ).Assembly.GetTypes()
					where !type.IsAbstract && type.IsClass && !type.IsGenericType
					where typeof( IRepository<TEntity> ).IsAssignableFrom( type )
					select type
				).FirstOrDefault();

			if( repositoryType == null )
				return null;

			return (IRepository<TEntity>)Activator.CreateInstance( repositoryType, unitOfWork );
		}
	}
}